package com.atguigu.day07;

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

public class Flink05_Union_WaterMark_Test {
    public static void main(String[] args) throws Exception {

            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);


            SingleOutputStreamOperator<Tuple2<String, Long>> stream1 = env
                    .socketTextStream("localhost", 8888)
                    .map(new MapFunction<String, Tuple2<String, Long>>() {

                        @Override
                        public Tuple2<String, Long> map(String value) throws Exception {
                            String[] arr = value.split(" ");
                            return Tuple2.of(arr[0], Long.parseLong(arr[1]) * 1000L);
                        }
                    })
                    .assignTimestampsAndWatermarks(
                            WatermarkStrategy.<Tuple2<String, Long>>forMonotonousTimestamps()
                                    .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                                        @Override
                                        public long extractTimestamp(Tuple2<String, Long> element, long recordTimestamp) {
                                            return element.f1;
                                        }
                                    })
                    );

            SingleOutputStreamOperator<Tuple2<String, Long>> stream2 = env
                    .socketTextStream("localhost", 7777)
                    .map(new MapFunction<String, Tuple2<String, Long>>() {
                        @Override
                        public Tuple2<String, Long> map(String value) throws Exception {
                            String[] arr = value.split(" ");
                            return Tuple2.of(arr[0], Long.parseLong(arr[1]) * 1000L);
                        }
                    })
                    .assignTimestampsAndWatermarks(
                            WatermarkStrategy.<Tuple2<String, Long>>forMonotonousTimestamps()
                                    .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                                        @Override
                                        public long extractTimestamp(Tuple2<String, Long> element, long recordTimestamp) {
                                            return element.f1;
                                        }
                                    })
                    );

            // 合并两条流进行处理
            stream1
                    .union(stream2)
                    .process(new ProcessFunction<Tuple2<String, Long>, String>() {
                        @Override
                        public void processElement(Tuple2<String, Long> in, Context context, Collector<String> collector) throws Exception {
                            collector.collect("当前水位线是：" + context.timerService().currentWatermark());
                        }
                    })
                    .print();

            env.execute();
        }

    }

